package com.example.accessdemo.domain;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;

import java.util.*;
import java.util.function.Predicate;

/**
 * @ClassName CommPage
 * @Description TODO
 * @Author kuangdda
 * @Date 11:51 2023/6/1
 * @Version 1.0
 **/
public class CommPage<T> implements IPage<T> {
    private static final long serialVersionUID = 8545996863226528798L;
    private List<T> records;
    private long total;
    private long pageSize;
    
    private long page;
    private List<OrderItem> orders;
    private boolean optimizeCountSql;
    private boolean isSearchCount;
    
    public CommPage() {
        this.records = Collections.emptyList();
        this.total = 0L;
        this.pageSize = 10L;
        this.page = 1L;
        this.orders = new ArrayList();
        this.optimizeCountSql = true;
        this.isSearchCount = true;
    }
    
    public CommPage(long current, long size) {
        this(current, size, 0L);
    }
    
    public CommPage(long current, long size, long total) {
        this(current, size, total, true);
    }
    
    public CommPage(long current, long size, boolean isSearchCount) {
        this(current, size, 0L, isSearchCount);
    }
    
    public CommPage(long current, long size, long total, boolean isSearchCount) {
        this.records = Collections.emptyList();
        this.total = 0L;
        this.pageSize = 10L;
        this.page = 1L;
        this.orders = new ArrayList();
        this.optimizeCountSql = true;
        this.isSearchCount = true;
        if (current > 1L) {
            this.page = current;
        }
        
        this.pageSize = size;
        this.total = total;
        this.isSearchCount = isSearchCount;
    }
    
    public boolean hasPrevious() {
        return this.page > 1L;
    }
    
    public boolean hasNext() {
        return this.page < this.getPages();
    }
    
    public List<T> getRecords() {
        return this.records;
    }
    
    public CommPage<T> setRecords(List<T> records) {
        this.records = records;
        return this;
    }
    
    public long getTotal() {
        return this.total;
    }
    
    public CommPage<T> setTotal(long total) {
        this.total = total;
        return this;
    }
    
    public long getSize() {
        return this.pageSize;
    }
    
    public CommPage<T> setSize(long size) {
        this.pageSize = size;
        return this;
    }
    
    public long getCurrent() {
        return this.page;
    }
    
    public CommPage<T> setCurrent(long current) {
        this.page = current;
        return this;
    }
    
    /** @deprecated */
    @Deprecated
    public String[] ascs() {
        return CollectionUtils.isNotEmpty(this.orders) ? this.mapOrderToArray(OrderItem::isAsc) : null;
    }
    
    private String[] mapOrderToArray(Predicate<OrderItem> filter) {
        List<String> columns = new ArrayList(this.orders.size());
        this.orders.forEach((i) -> {
            if (filter.test(i)) {
                columns.add(i.getColumn());
            }
            
        });
        return (String[])columns.toArray(new String[0]);
    }
    
    private void removeOrder(Predicate<OrderItem> filter) {
        for(int i = this.orders.size() - 1; i >= 0; --i) {
            if (filter.test(this.orders.get(i))) {
                this.orders.remove(i);
            }
        }
        
    }
    
    public CommPage<T> addOrder(OrderItem... items) {
        this.orders.addAll(Arrays.asList(items));
        return this;
    }
    
    public CommPage<T> addOrder(List<OrderItem> items) {
        this.orders.addAll(items);
        return this;
    }
    
    /** @deprecated */
    @Deprecated
    public CommPage<T> setAscs(List<String> ascs) {
        return CollectionUtils.isNotEmpty(ascs) ? this.setAsc((String[])ascs.toArray(new String[0])) : this;
    }
    
    /** @deprecated */
    @Deprecated
    public CommPage<T> setAsc(String... ascs) {
        this.removeOrder(OrderItem::isAsc);
        String[] var2 = ascs;
        int var3 = ascs.length;
        
        for(int var4 = 0; var4 < var3; ++var4) {
            String s = var2[var4];
            this.addOrder(OrderItem.asc(s));
        }
        
        return this;
    }
    
    /** @deprecated */
    @Deprecated
    public String[] descs() {
        return this.mapOrderToArray((i) -> {
            return !i.isAsc();
        });
    }
    
    /** @deprecated */
    @Deprecated
    public CommPage<T> setDescs(List<String> descs) {
        if (CollectionUtils.isNotEmpty(descs)) {
            this.removeOrder((item) -> {
                return !item.isAsc();
            });
            Iterator var2 = descs.iterator();
            
            while(var2.hasNext()) {
                String s = (String)var2.next();
                this.addOrder(OrderItem.desc(s));
            }
        }
        
        return this;
    }
    
    /** @deprecated */
    @Deprecated
    public CommPage<T> setDesc(String... descs) {
        this.setDescs(Arrays.asList(descs));
        return this;
    }
    
    public List<OrderItem> orders() {
        return this.getOrders();
    }
    
    public List<OrderItem> getOrders() {
        return this.orders;
    }
    
    public void setOrders(List<OrderItem> orders) {
        this.orders = orders;
    }
    
    public boolean optimizeCountSql() {
        return this.optimizeCountSql;
    }
    
    public boolean isSearchCount() {
        return this.total < 0L ? false : this.isSearchCount;
    }
    
    public CommPage<T> setSearchCount(boolean isSearchCount) {
        this.isSearchCount = isSearchCount;
        return this;
    }
    
    public CommPage<T> setOptimizeCountSql(boolean optimizeCountSql) {
        this.optimizeCountSql = optimizeCountSql;
        return this;
    }
}
